//
// Created by hkkzzxz on 24-9-4.
//

#include "BaseController.h"

bool BaseController::parseRequestBody(const http::request<http::dynamic_body> &httpRequest, Json::Value &root) {
    auto &body = httpRequest.body();
    auto bodyStr = boost::beast::buffers_to_string(body.data());
    consoleLogger->info("httpRequest bodyStr:{}", bodyStr);

    Json::Reader reader;
    bool parsingSuccessful = reader.parse(bodyStr, root);
    if (!parsingSuccessful) {
        consoleLogger->info("Failed to parse JSON data!");
        return false;
    }
    return true;
}

void BaseController::setHttpResponse(http::response<http::dynamic_body> &httpResponse, const Json::Value &root) {
    httpResponse.set(http::field::content_type, "text/json");
    std::string jsonStr = root.toStyledString();
    consoleLogger->info("httpResponse bodyStr:{}", jsonStr);
    beast::ostream(httpResponse.body()) << jsonStr;
}